<script setup>
import FirstTitle from "@comp/FirstTitle";
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
import { onMounted } from "vue";
import { searchDataRyfh } from "@/api/search";
import { addOrUpdateRyfh } from "@/api/addOrUpdate";

const city = window.globalObj.name;
const editorRef = shallowRef();

const toolbarConfig = {};
const editorConfig = {
  placeholder: "请输入内容...",
};
const mode = ref("default");

// 加载html
const initHtml = () => {
  searchDataRyfh({ m: "行动要点-人口疏散行动" }).then((res) => {
    const { data } = res;
    valueHtml.value = data.find((x) => x.k === "人口疏散行动")?.v || "";
  });
};

const handleCreated = (editor) => {
  editorRef.value = editor; // 记录 editor 实例，重要！
};

const valueHtml = ref(
  "疏散阶段划分：早期疏散、临战疏散、紧急疏散三个阶段。<br/>全市疏散人口数量约为XX万人，留城人口数约为XX万人。<br/>第一阶段：早期疏散<br/>疏散时机：人防指挥部依照上级命令宣布进入二级战备时。<br/>疏散完成时间：10天。<br/>疏散对象：一是动员外来务工人员离城、返乡，动员能投亲靠友城市人员离开城市；二是重点疏散学校和重要科研机构及科技人员，幼、病、残、孕人员，闲散无业人员、外来暂住人员及在押犯人、重要目标毗连区、易产生次生灾害目标周围的人员等。<br/>第二阶段：临战疏散<br/>疏散时机：国家宣布进入战争状态后至战争爆发。<br/>疏散完成时间：3天。<br/>疏散对象：城市中心区、人口密集区、商业繁华区、重要目标毗连区、易产生次生灾害目标周围的人员。<br/>第三阶段：紧急疏散<br/>疏散时机：发现敌明显空袭征候，形势十分紧张时。<br/>疏散完成时间：6小时内。<br/>疏散对象：可能遭受打击地域的居民和留城、留守的“三坚持”人员<br/>"
);

const saveProgramme = () => {
  ElMessageBox.confirm(`确定要保存吗?`, "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
    customClass: "message-box",
  }).then(() => {
    addOrUpdateRyfh({
      m: "行动要点-人口疏散行动",
      data: [
        {
          k: "人口疏散行动",
          v: valueHtml.value,
        },
      ],
    })
      .then(() => {
        ElMessage.success("操作成功");
      })
      .catch(() => {});
  });
};

onMounted(() => {
  initHtml();
});
onBeforeUnmount(() => {
  const editor = editorRef.value;
  if (!editor) return;
  editor.destroy();
});
</script>
<template>
  <div class="">
    <FirstTitle name="人口疏散行动" />
    <div style="border: 1px solid #ccc" class="top-15">
      <Toolbar
        style="border-bottom: 1px solid #ccc"
        :editor="editorRef"
        :defaultConfig="toolbarConfig"
        :mode="mode"
      />
      <Editor
        style="height: 500px; overflow-y: hidden"
        v-model="valueHtml"
        :defaultConfig="editorConfig"
        :mode="mode"
        @onCreated="handleCreated"
      />
    </div>

    <div class="flex-justify-end top-20">
      <el-button type="primary" class="save-btn" @click="saveProgramme"
        >保存</el-button
      >
    </div>
  </div>
</template>
<style scoped lang="scss"></style>